//
//  UIView+WaitingView.m
//  CRMSystemForHall
//
//  Created by  丁丁 on 13-6-8.
//
//

#import "UIView+WaitingView.h"
#import <objc/runtime.h>
#import "LSProgressHUD.h"

#define WaitViewKey "__WaitViewKey"

@interface DDWaitView : UIView

@property (nonatomic,strong) UIActivityIndicatorView *active;

@property (nonatomic,strong) UILabel *label;

@property (nonatomic,strong) UIImageView *contentView;

-(void)showWait:(BOOL)isShow;

@end

@implementation DDWaitView

-(id)initWithFrame:(CGRect)frame{

    if (self =[super initWithFrame:frame]) {
        
        self.userInteractionEnabled =YES;
        
//        self.backgroundColor =[UIColor colorWithRed:115./255. green:115./255. blue:115./255. alpha:0.6];
        self.backgroundColor =[UIColor clearColor];
        self.contentView =[[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-50)/2, (frame.size.height-50)/2, 50, 50)];
        _contentView.backgroundColor =[UIColor clearColor];
//        _contentView.layer.cornerRadius =5.0;
        
        [LSProgressHUD showToView:self message:nil];
//        [self addSubview:_contentView];
//        self.contentView.animationImages = [NSArray arrayWithObjects:
//                                            [UIImage imageNamed:@"imgofwaitview3.png"],
//                                            [UIImage imageNamed:@"imgofwaitview2.png"],
//                                            [UIImage imageNamed:@"imgofwaitview1.png"],
//                                            nil];
//        [self.contentView setAnimationDuration:3.0];
//        [self.contentView setAnimationRepeatCount:9999];
        
//        self.active =[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
//        _active.center =CGPointMake(CGRectGetWidth(_active.frame) +5, CGRectGetHeight(_contentView.frame)/2);
//        _active.color =[UIColor lightGrayColor];
//        [_contentView addSubview:_active];
        
//        self.label =[[UILabel alloc] initWithFrame:CGRectMake(30, self.contentView.center.y + 40, CGRectGetWidth(frame) - 60, 30)];
//        _label.backgroundColor =[UIColor clearColor];
//      //  _label.textColor =[UIColor blueColor];
//        _label.textColor =[UIColor colorWithRed:0.302 green:0.467 blue:0.733 alpha:1.000];
//        [_label setTextAlignment:NSTextAlignmentCenter];
//        _label.font =[UIFont systemFontOfSize:14.0f];
//        _label.text =@"请稍后，正在加载中...";
//        [self addSubview:_label];
    }
    return self;
}

-(void)showWait:(BOOL)isShow{

    if (isShow) {
        [self.contentView startAnimating];

        self.hidden =NO;
//        [_active startAnimating];
    }
    else{
        [self.contentView stopAnimating];

        self.hidden =YES;
//        [_active stopAnimating];
    }
}

-(void)layoutSubviews{

    self.frame =CGRectMake(0, 64, CGRectGetWidth(self.superview.frame), CGRectGetHeight(self.superview.frame) - 64 - 49);
    
    _contentView.center =CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2 - 40);
    
    CGPoint center = _label.center;
    center.y = _contentView.center.y + 50;
    _label.center = center;
    
    [super layoutSubviews];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}

@end

@implementation UIView (WaitingView)

-(void)setWaitView:(UIView *)waitView{

     objc_setAssociatedObject(self, WaitViewKey, waitView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(UIView *)waitView{

    return objc_getAssociatedObject(self, WaitViewKey);
}

-(void)showWait:(BOOL)isShow{

    UIView *viewOfWait =[self waitView];
    
    if (isShow) {
        [self startTimer];
        if (viewOfWait ==nil) {
            viewOfWait =[[DDWaitView alloc] initWithFrame:CGRectMake(0, 64, self.bounds.size.width, self.bounds.size.height)];
            viewOfWait.backgroundColor = [UIColor clearColor];
            [self setWaitView:viewOfWait];
            [self addSubview:viewOfWait];
        }
        [viewOfWait showWait:YES];
        
    }
    else{
        if (viewOfWait !=nil) {
            [viewOfWait showWait:NO];
            [viewOfWait removeFromSuperview];
            [self setWaitView:nil];
        }
    }
}

-(void)startTimer{
    
    [NSTimer scheduledTimerWithTimeInterval:120.0f target:self selector:@selector(hidenWaitView) userInfo:nil repeats:NO];
}

-(void)hidenWaitView{
    [self showWait:NO];
}



@end
